package cn.tedu.adminsystem.webapi.controller;
import cn.tedu.adminsystem.service.PhotoUploadService;
import cn.tedu.community.commons.restful.JsonResult;
import io.swagger.annotations.ApiOperation;
import cn.tedu.community.commons.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;

/**
 * 管理员模块图片上传控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class UploadController {

    @Autowired
    private PhotoUploadService service;

    @ApiOperation("图片上传操作")
    @PostMapping("/upload")
    public String getPicFileName(MultipartFile file) {
        return service.getPicFileName(file);
    }

    @ApiOperation("图片删除操作")
    @PostMapping("/uploadDelete/uuid={uuid}")
    public JsonResult<Void> getPicFileName(@PathVariable @NotEmpty(message = "服务器繁忙，请重新选择图片！") String uuid) {
        log.debug("控制层开始执行图片删除操作，图片uuid值是：{}",uuid);
        service.deletePhoto(uuid);
        return JsonResult.ok();
    }
}
